package com.jufanshare.myblog.service.record;

import com.jufanshare.myblog.common.dto.AjaxRes;
import com.jufanshare.myblog.common.util.VerifyCodeUtils;
import com.jufanshare.myblog.dao.RecordDao;
import com.jufanshare.myblog.dao.email.EmailRepository;
import com.jufanshare.myblog.entity.Record;
import com.jufanshare.myblog.entity.User;
import com.jufanshare.myblog.service.base.BaseServiceImpl;
import com.jufanshare.myblog.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @功能：
 * @开发者： 大BUG
 * @编写时间： 2019/2/2 10:05
 */
@Service
public class RecordServiceImpl extends BaseServiceImpl<Record> implements RecordService {
    @Autowired
    private EmailRepository emailRepository;
    @Autowired
    private RecordDao recordDao;
    @Autowired
    private UserService userService;
    @Override
    public AjaxRes registerRecord(String toEmail,String ip, HttpSession session) {
        AjaxRes res=new AjaxRes();
        Record record=new Record();
        record.setRecordtype("register_email_code_record");
        record.setRecordvalue(ip);
        long count = templateCount(record);
        User user=new User();
        user.setEmail(toEmail);
        long count1 = userService.templateCount(user);
        if(count1>0){
            res.setFailMsg("此邮箱已注册，请【登录】或【找回密码】");
            return res;
        }
        if(count>=3){
            res.setFailMsg("今日发送验证码次数过多，请明日再试");
            return res;
        }
        String code = VerifyCodeUtils.generateVerifyCode(6);//生成6位验证码
        try {
            emailRepository.sendEmailCode(toEmail,code);
            record.setRecorddatetime(new Date());
            insert(record);
            session.setAttribute("registeremailcode",toEmail+code);//把邮箱和验证码存入session，以待后面验证
            res.setRes(true);
        }catch (Exception e){
            res.setFailMsg("服务器错误："+e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public AjaxRes resetRecord(String toEmail, String ip, HttpSession session) {
        AjaxRes res=new AjaxRes();
        Record record=new Record();
        record.setRecordtype("reset_email_code_record");
        record.setRecordvalue(ip);
        long count = templateCount(record);
        User user=new User();
        user.setEmail(toEmail);
        long count1 = userService.templateCount(user);
        if(count1==0){
            res.setFailMsg("此邮箱未注册，请先【注册】");
            return res;
        }
        if(count>=3){
            res.setFailMsg("今日发送验证码次数过多，请明日再试");
            return res;
        }
        String code = VerifyCodeUtils.generateVerifyCode(6);//生成6位验证码
        try {
            emailRepository.sendEmailCode(toEmail,code);
            record.setRecorddatetime(new Date());
            insert(record);
            session.setAttribute("resetemailcode",toEmail+code);//把邮箱和验证码存入session，以待后面验证
            res.setRes(true);
        }catch (Exception e){
            res.setFailMsg("服务器错误："+e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int clearRecord(Record record) {
        return recordDao.clearRecord(record);
    }
}
